package cn.appsys.controller.backend;

import cn.appsys.pojo.AppCategory;
import cn.appsys.pojo.BackendUser;
import cn.appsys.service.developer.AppCategoryService;
import cn.appsys.tools.Constants;
import cn.appsys.tools.PageSupport;
import cn.appsys.tools.ReturnObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manager/backend/appclassification")
public class appclassificationController {
    @Autowired
    private AppCategoryService appCategoryService;
    @RequestMapping("/toAppClassification")
    public String toAppClassification( String pageIndex,String queryCategoryLevel1,String queryCategoryLevel2,String queryCategoryLevel3,Model model){
        System.out.println("这是queryCategoryLevel1"+queryCategoryLevel1);
        String parentId = null;
        System.out.println(parentId);
        if(queryCategoryLevel1!=null&&!queryCategoryLevel1.equals("")){
            parentId = queryCategoryLevel1;
        }
        if (queryCategoryLevel2!=null&&!queryCategoryLevel2.equals("")) {
            parentId = queryCategoryLevel2;
        }
        if(queryCategoryLevel3!=null&&!queryCategoryLevel3.equals("")){
            parentId = queryCategoryLevel3;
        }
        System.out.println(parentId);
        Map<String,Object> map = new HashMap<>();
        int pageSize = Constants.pageSize;
        //设置当前页码
        Integer currentPageNo = 1;
        if (pageIndex != null) {
            try {
                currentPageNo = Integer.valueOf(pageIndex);
            } catch (NumberFormatException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
        map.put("beginNo", (currentPageNo - 1) * pageSize);
        map.put("pageSize", pageSize);
        map.put("parentId", parentId);
        int totalCount = 0;

        try {
            totalCount = appCategoryService.queryCategoryCountByConditionForPage(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageSupport pages = new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        //校验页码
        if (currentPageNo < 1) {
            currentPageNo = 1;
        } else if (currentPageNo > totalPageCount) {
            currentPageNo = totalPageCount;
        }

        List<AppCategory> appCategoryList = appCategoryService.queryAllCategory(map);
        List<AppCategory> categoryLevel1List = getCategoryList(null);
        model.addAttribute("appCategoryList",appCategoryList);
        model.addAttribute("categoryLevel1List",categoryLevel1List);
        model.addAttribute("queryCategoryLevel1", queryCategoryLevel1);
        model.addAttribute("queryCategoryLevel2", queryCategoryLevel2);
        model.addAttribute("pages", pages);

        List<AppCategory> categoryLevel2List = null;
        List<AppCategory> categoryLevel3List = null;
        if(queryCategoryLevel1 != null && !queryCategoryLevel1.equals("")){
            categoryLevel2List = getCategoryList(queryCategoryLevel1.toString());
            model.addAttribute("categoryLevel2List", categoryLevel2List);
        }
        if(queryCategoryLevel2 != null && !queryCategoryLevel2.equals("")){
            categoryLevel3List = getCategoryList(queryCategoryLevel2.toString());
            model.addAttribute("categoryLevel3List", categoryLevel3List);
        }
        return "backend/appclassification";
    }

    public List<AppCategory> getCategoryList (String parentId){
        List<AppCategory> categoryLevelList = null;
        try {
            categoryLevelList = appCategoryService.getAppCategoryListByParentId(parentId==null?null:Integer.parseInt(parentId));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return categoryLevelList;
    }


    /**
     * 根据parentId查询出相应的分类级别列表
     * @param pid
     * @return
     */
    @RequestMapping(value="/categorylevellist.json",method= RequestMethod.GET)
    @ResponseBody
    public List<AppCategory> getAppCategoryList (@RequestParam String pid){
        if(pid.equals("")) pid = null;
        return getCategoryList(pid);
    }

    @RequestMapping("/saveAppCategory")
    @ResponseBody
    public Object saveAppCategory(HttpSession session,AppCategory appCategory,int parentId){
        BackendUser backendUser = (BackendUser) session.getAttribute(Constants.USER_SESSION);
        Date date = new Date();
        ReturnObject returnObject = new ReturnObject();
        appCategory.setCreatedBy(backendUser.getId());
        appCategory.setParentId(parentId);
        appCategory.setCreationDate(date);
        try {
            int ret = appCategoryService.saveCreatAppCategory(appCategory);
            if(ret>0){
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
            }else {
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("添加失败，请稍后再试！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("添加失败，请稍后再试！");
        }
        return returnObject;
    }

    @RequestMapping("/toAddAppclassification")
    public String toAddAppclassification(String id,Model model){
        AppCategory appCategory = appCategoryService.queryAppCategoryById(id);
        model.addAttribute("appCategory",appCategory);
        return "backend/addclassification";
    }
}
